package cn.blue.base.service.impl;

import cn.blue.base.config.BussinessException;
import cn.blue.base.constant.BaseConstants;
import cn.blue.base.service.IRegisterService;
import cn.blue.base.util.MD5Utils;
import cn.blue.base.util.StrUtils;
import cn.blue.user.domain.Logininfo;
import cn.blue.user.domain.User;
import cn.blue.user.dto.PhoneRegisterDto;
import cn.blue.user.mapper.LogininfoMapper;
import cn.blue.user.mapper.UserMapper;
import jdk.nashorn.internal.runtime.options.LoggingOption;
import org.apache.commons.lang3.StringUtils;
import org.apache.xmlbeans.impl.xb.xsdschema.Attribute;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import sun.security.util.Length;

import java.util.Date;

@Service
public class RegisterServiceImpl implements IRegisterService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private LogininfoMapper logininfoMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public void phoneRegister(PhoneRegisterDto dto) {
        //验证参数
        this.verifyParam(dto);
        //插
        User user = dtoConvertUser(dto);
        Logininfo logininfo = userConvertLogininfo(user);
        //添加
        logininfoMapper.add(logininfo);
        user.setLogininfoId(logininfo.getId());
        userMapper.add(user);

    }
    private Logininfo userConvertLogininfo(User user){
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(user,logininfo);
        return logininfo;

    }
    private User dtoConvertUser(PhoneRegisterDto dto){
        String salt = StrUtils.getComplexRandomString(32);
        //密码
        String password = salt + dto.getPassword();
        String encryPwd =  MD5Utils.encrypByMd5(password);
        return User.builder()
                .username(dto.getPhone())
                .phone(dto.getPhone())
                .salt(salt)
                .password(encryPwd)
                .state(1)
                .createtime(new Date())
                .build();
    }

    private void verifyParam(PhoneRegisterDto dto) {
        //判断非空
        //俩次密码是否一致
        if(!dto.getPassword().equals(dto.getPasswordRepeat())){
            throw new BussinessException("俩次密码不一致");
        }
        //判断验证码是否正确/俩次手机号是否一致
        String redisSmsCode = (String) redisTemplate.opsForValue().get(String.format(BaseConstants.VerfityCodeCons.REGISTER_SMSCODE_PHONE, dto.getPhone()));
        if (StringUtils.isBlank(redisSmsCode)){
            throw new BussinessException("验证码错误");
        }
        String[] smsCode  = redisSmsCode.split(":");
        if (!smsCode[0].equals(dto.getSmsCode())){
            throw new BussinessException("验证码错误");
        }
        //是否注册过
        User user =  userMapper.findByPhone(dto.getPhone());
        if (null != user){
            throw new BussinessException("滚去登录");
        }
    }
}
